###抽取所有数据历史数据（按时间）重要
import requests
import random
import time
import json

url = "https://mt.fjii.com/api/wechatShareTask/list"
# payload="title=&serviceId=1&pageSize=10000&pageNo=1&start_time=2022-01-01%2018%3A35%3A00&end_time=2022-01-31%2018%3A36%3A00&token_header=xOjqTc9vix%2FIQVwVMZmkpCwp4B2WafoeT991Rnx%2FJhmYF6H9Xv46EkHqmsU%2BjVVIZyeIygoJ8sDqdM7d4AfPy19p0ULjujQFFQMUEzFwNVRNw%2FJo8ou6CGPkfB%2B%2FLMlgX2y%2BudFF9SrsZ5kWvffVJg%3D%3D"
# 注意这里有start_time 和end_time ,这个就是需要获取哪段时间的每条通信头条的id
# token_header是你的token，需要用Fiddler获取，注意的是通信头条会每个几天更新token，更新后你得重新用Fiddler对token进行获取更新到这里
# payload="serviceId=1&secondaryColumn=1333926968260956160&pageSize=10&pageNo=2"

headers = {
    'Host': 'mt.fjii.com',
    'Connection': 'keep-alive',
    'Content-Length': '263',
    'Accept': 'application/json, text/plain, */*',
    'User-Agent': 'Mozilla/5.0 (Linux; Android 10; ALP-AL00 Build/HUAWEIALP-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36wpsystem',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Origin': 'https://mt.fjii.com',
    'X-Requested-With': 'com.fuzhou.wpsystem',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Cookie': 'token=38bc9209-6525-4a8b-9464-62837cb64097'

}
from datetime import datetime, timedelta

# 获取今天的日期
today = datetime.now()
# 格式化今天的日期
formatted_today = today.strftime('%Y-%m-%d')
# 获取昨天的日期
yesterday = today - timedelta(days=1)
# 格式化昨天的日期
formatted_yesterday = yesterday.strftime('%Y-%m-%d')
print(formatted_today)
print(formatted_yesterday)

# 第一次请求，获取总页数
payload = f"serviceId=1&secondaryColumn=&pageSize=10&pageNo=1&start_time={formatted_yesterday}%2000%3A20%3A00&end_time={formatted_today}%2000%3A20%3A00"
print(payload)
# payload = "serviceId=3&secondaryColumn=&pageSize=10&pageNo=1"
response = requests.request("POST", url, headers=headers, data=payload)
response_text = response.text
# print(response_text)
fistRequest = json.loads(response_text)
# data = fistRequest['data']['list']
total = fistRequest['data']['total']
print('total:', total)
# print(data)   # 打印出来看看

# 利用总页数，循环请求
result = []
lastNum = int(total / 10)
for page_number in range(1, lastNum):
    payload = f"serviceId=3&secondaryColumn=&pageSize=10&pageNo={page_number}&start_time={formatted_yesterday}%2000%3A20%3A00&end_time={formatted_today}%2000%3A20%3A00"
    response = requests.request("POST", url, headers=headers, data=payload)
    response_text = response.text
    data = json.loads(response_text)
    print('page_number:', page_number)
    # print(data['data']['list'])
    temp = data['data']['list']
    # 遍历temp数组里面的每一个对象，取出id
    for item in temp:
        result.append(item['id'])
    # result.extend(data['data']['list'])
    time.sleep(1)
# 将所有的id存入json文件
with open('ids.json', 'w') as f:
    json.dump(result, f)

# print(result)
